
#include "MNIST.h"

void datasets_mnist::CImageSet::myInit(
	string strFN_ListImage,
	string strFN_ListLabel )
{
	unsigned char zz[4];
	FILE* FIN;

	FIN = fopen(strFN_ListImage.c_str(), "rb");
	CDataStructureTools::my_fread(FIN, zz, 4);
	CDataStructureTools::my_fread(FIN, zz, 4);
	AmountImage = (int)(zz[3]) 
		| (((int)(zz[2])) << 8)
		| (((int)(zz[1])) << 16)
		| (((int)(zz[0])) << 24);
	CDataStructureTools::my_fread(FIN, zz, 4);
	W = (int)(zz[3]) 
		| (((int)(zz[2])) << 8)
		| (((int)(zz[1])) << 16)
		| (((int)(zz[0])) << 24);
	CDataStructureTools::my_fread(FIN, zz, 4);
	H = (int)(zz[3]) 
		| (((int)(zz[2])) << 8)
		| (((int)(zz[1])) << 16)
		| (((int)(zz[0])) << 24);
	WH = W * H;
	CDataStructureTools::my_fread(FIN, ListPixel, WH * AmountImage);
	fclose(FIN);

	FIN = fopen(strFN_ListLabel.c_str(), "rb");
	CDataStructureTools::my_fread(FIN, zz, 4);
	CDataStructureTools::my_fread(FIN, zz, 4);
	CDataStructureTools::my_fread(FIN, ListLabel, AmountImage);
	fclose(FIN);
}

int datasets_mnist::CImageSet::GetImage( int ImageIndex, CImg<int>& imgTarget )
{
	if (ImageIndex<0 || ImageIndex>=AmountImage) return -1;
	unsigned char* qSrc = ListPixel.pElement
		+ 0 + ((size_t)WH) * (size_t)ImageIndex;
	imgTarget.assign(qSrc, W, H, 1, 1, false);
	return ListLabel.pElement[ImageIndex];
}

